home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-25  |  6.3 KB  |  225 lines

  1.  
  2.  
  3.     /***********************************************
  4.     *
  5.     *       file d:\cips\rotate.c
  6.     *
  7.     *       Functions: This file contains
  8.     *          rotate_flip_image_array
  9.     *
  10.     *       Purpose:
  11.     *          This function rotates or flips an image
  12.     *          array in one of five ways.
  13.     *
  14.     *       External Calls:
  15.     *          wtiff.c - round_off_image_size
  16.     *                    create_file_if_needed
  17.     *                    write_array_into_tiff_image
  18.     *          tiff.c - read_tiff_header
  19.     *          rtiff.c - read_tiff_image
  20.     *
  21.     *
  22.     *       Modifications:
  23.     *          1 April 1992 - created
  24.     *
  25.     *************************************************/
  26.  
  27. #include "cips.h"
  28.  
  29.      /*******************************************
  30.      *
  31.      *   rotate_flip_image_array(...
  32.      *
  33.      *   This function rotates an image array
  34.      *   in one of three ways or flips an image
  35.      *   array either vertically or horizontally.
  36.      *   The rotation_type parameter specifies
  37.      *   the operation.  When rotation_type is
  38.      *   1, 2, or 3 you rotate.  When it is
  39.      *   4 or 5 you flip.
  40.      *
  41.      *   I define rotation as this:  Pin down the
  42.      *   lower left hand corner of the image array
  43.      *   and rotate the image 90 degrees clockwise.
  44.      *   1 rotation is 90 degrees, 2 rotations are
  45.      *   180 degrees, and 3 rotations are 270 degrees.
  46.      *   4 rotations bring you back to where you
  47.      *   started.
  48.      *
  49.      *   The cases are:
  50.      *
  51.      *   If the input image array is:
  52.      *        1 2 3
  53.      *        4 5 6
  54.      *        7 8 9
  55.      *
  56.      *   Rotate # 1 - the result is:
  57.      *        7 4 1
  58.      *        8 5 2
  59.      *        9 6 3
  60.      *
  61.      *   Rotate # 2 - the result is:
  62.      *        9 8 7
  63.      *        6 5 4
  64.      *        3 2 1
  65.      *
  66.      *   Rotate # 3 - the result is:
  67.      *        3 6 9
  68.      *        2 5 8
  69.      *        1 4 7
  70.      *
  71.      *   Flip # 4 - horizontal the result is:
  72.      *        3 2 1
  73.      *        6 5 4
  74.      *        9 8 7
  75.      *
  76.      *   Flip # 5 - vertical the result is:
  77.      *        7 8 9
  78.      *        4 5 6
  79.      *        1 2 3
  80.      *
  81.      *
  82.      *   The in_file is the source image with
  83.      *   parameters given by il1, ie1, ll1, le1.
  84.      *
  85.      *   The out_file is the destination image with
  86.      *   parameters given by il2, ie2, ll2, le2.
  87.      *
  88.      *******************************************/
  89.  
  90.  
  91. rotate_flip_image_array(in_name, out_name, the_image,
  92.                    out_image, il1, ie1, ll1, le1,
  93.                    il2, ie2, ll2, le2, rotation_type)
  94.    char  in_name[], out_name[];
  95.    int   il1, ie1, ll1, le1,
  96.          il2, ie2, ll2, le2, rotation_type;
  97.    short the_image[ROWS][COLS],
  98.          out_image[ROWS][COLS];
  99. {
  100.    int    cd2, i, j, length, rd2, type, width;
  101.    struct tiff_header_struct image_header;
  102.  
  103.    create_file_if_needed(in_name, out_name, out_image);
  104.  
  105.      /*******************************************
  106.      *
  107.      *   Check the rotation_type.  If it is not
  108.      *   a valid value, set it to 1.
  109.      *
  110.      *******************************************/
  111.  
  112.    type = rotation_type;
  113.    if(type != 1  &&
  114.       type != 2  &&
  115.       type != 3  &&
  116.       type != 4  &&
  117.       type != 5) type = 1;
  118.  
  119.    read_tiff_image(in_name, the_image, 
  120.                    il1, ie1, ll1, le1);
  121.  
  122.      /*******************************************
  123.      *
  124.      *   Rotate the image array as desired.
  125.      *
  126.      *******************************************/
  127.  
  128.      /*******************************************
  129.      *
  130.      *   1 90 degree rotation
  131.      *
  132.      *******************************************/
  133.  
  134.    if(type == 1  ||  type == 2  ||  type == 3){
  135.       for(i=0; i<ROWS; i++){
  136.          for(j=0; j<COLS; j++)
  137.             out_image[j][COLS-1-i] = the_image[i][j];
  138.       }  /* ends loop over i */
  139.    }  /* ends if type == 1 or 2 or 3 */
  140.  
  141.  
  142.      /*******************************************
  143.      *
  144.      *   a second 90 degree rotation
  145.      *
  146.      *******************************************/
  147.  
  148.    if(type == 2  ||  type == 3){
  149.       for(i=0; i<ROWS; i++)
  150.          for(j=0; j<COLS; j++)
  151.             the_image[i][j] = out_image[i][j];
  152.       for(i=0; i<ROWS; i++){
  153.          for(j=0; j<COLS; j++)
  154.             out_image[j][COLS-1-i] = the_image[i][j];
  155.       }  /* ends loop over i */
  156.    }  /* ends if type == 2 or 3 */
  157.  
  158.  
  159.      /*******************************************
  160.      *
  161.      *   a third 90 degree rotation
  162.      *
  163.      *******************************************/
  164.  
  165.    if(type == 3){
  166.       for(i=0; i<ROWS; i++)
  167.          for(j=0; j<COLS; j++)
  168.             the_image[i][j] = out_image[i][j];
  169.       for(i=0; i<ROWS; i++){
  170.          for(j=0; j<COLS; j++)
  171.             out_image[j][COLS-1-i] = the_image[i][j];
  172.       }  /* ends loop over i */
  173.    }  /* ends if type == 3 */
  174.  
  175.  
  176.      /*******************************************
  177.      *
  178.      *   Flip the image array horizontally
  179.      *   about the center vertical axis.
  180.      *
  181.      *******************************************/
  182.  
  183.    if(type == 4){
  184.       cd2 = COLS/2;
  185.       for(j=0; j<cd2; j++){
  186.          for(i=0; i<ROWS; i++){
  187.             out_image[i][COLS-1-j] = the_image[i][j];
  188.          }  /* ends loop over i */
  189.       }  /* ends loop over j */
  190.  
  191.       for(j=cd2; j<COLS; j++){
  192.          for(i=0; i<ROWS; i++){
  193.             out_image[i][COLS-1-j] = the_image[i][j];
  194.          }  /* ends loop over i */
  195.       }  /* ends loop over j */
  196.    }  /* ends if type == 4 */
  197.  
  198.  
  199.      /*******************************************
  200.      *
  201.      *   Flip the image array vertically
  202.      *   about the center horizontal axis.
  203.      *
  204.      *******************************************/
  205.  
  206.    if(type == 5){
  207.       rd2 = ROWS/2;
  208.       for(i=0; i<rd2; i++){
  209.          for(j=0; j<COLS; j++){
  210.             out_image[ROWS-1-i][j] = the_image[i][j];
  211.          }  /* ends loop over j */
  212.       }  /* ends loop over i */
  213.  
  214.       for(i=rd2; i<ROWS; i++){
  215.          for(j=0; j<COLS; j++){
  216.             out_image[ROWS-1-i][j] = the_image[i][j];
  217.          }  /* ends loop over j */
  218.       }  /* ends loop over i */
  219.    }  /* ends if type == 5 */
  220.  
  221.    write_array_into_tiff_image(out_name, out_image,
  222.                                il2, ie2, ll2, le2);
  223.  
  224. }  /* ends rotate_flip_image_array */
  225.